import { ContextWithMiddleware } from './ContextWithMiddleware';
import type { TypedKey } from './types';

export class SafeContext extends ContextWithMiddleware {
  setData<T>(key: TypedKey<T>, value: T, immediate = true): void {
    try {
      super.setData(key, value, immediate);
    } catch (error) {
      console.error(`Error setting data for key ${key.key}:`, error);
      // 可以集成到Sentry等错误监控系统
    }
  }

  getData<T>(key: TypedKey<T>): T {
    try {
      return super.getData(key);
    } catch (error) {
      console.error(`Error getting data for key ${key.key}:`, error);
      return key.defaultValue;
    }
  }
}
